import utils.TreeNode;

/**
 * @Author ZhangCuirong
 * @Date 2025/9/8 13:50
 * @description:
 */
public class Solution572 {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(subRoot == null){
            return true;
        }else if(root==null){
            return false;
        }else{
            if(isSameTree(root,subRoot)){
                return true ;
            }else{
                return isSubtree(root.left, subRoot)||isSubtree(root.right,subRoot);
            }
        }

    }
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        } else if (p != null && q != null) {
            if (p.val != q.val) {
                return false;
            } else {
                return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
            }
        } else {
            return false;
        }
    }
}
